{extend name="platform/base" /}
{block name="resources"/}
<link rel="stylesheet" type="text/css" href="ADMIN_CSS/product.css">
<script src="ADMIN_JS/material_managedialog.js"></script>
<link rel="stylesheet" type="text/css"  href="__STATIC__/loading/msgbox.css" />
<script type="text/javascript" src="__STATIC__/loading/msgbox.js"></script>
<style type="text/css">
.fun-a{
	margin-top: 0px;
	padding: 2px 15px;
	border: 1px solid #cccccc;
	background-color: #f5f5f5;
}
.mytable{border-bottom:1px solid #f1f1f1; line-height: 45px; padding-left: 5px;}
.mytable th{padding-left: 5px;}
.right-indent:after{
	content:'';
	display:inline-block;
	width:20%;
}
</style>
{/block}
{block name="main"}
<section class="panel panel-default">
	<header class="panel-heading font-bold"> 数据库管理 </header>
	<table class="mytable">
		<tr>
			<th>
				<a class="btn btn-small fun-a" href="javascript:batchDelete()">备份</a>
				<!-- <a class="btn btn-small fun-a" href="javascript:makeTable(2)">优化</a> -->
				<a class="btn btn-small fun-a" href="javascript:makeTable(1)">修复</a>
			</th>
		</tr>
	</table>


	<table class="table table-striped b-light text-sm">
		 <colgroup>
			<col style="width: 1%">
			<col style="width: 25%;">
			<col style="width: 15%;">
			<col style="width: 15%;">
			<col style="width: 24%;">
			<col style="width: 20%;">
		</colgroup>
		<thead>
			<tr align="center">
				<th><label><input id="ckall" type="checkbox" onClick="CheckAll(this)"></label></th>
				<th >表名</th>
				<th class="right right-indent">数据量</th>
				<th class="right right-indent">数据大小</th>
				<th class="center">创建时间</th>
				<th class="center">操作</th>
			</tr>
		</thead>
		<tbody></tbody>
	</table>
</section>
{/block}
{block name="script"}
<script type="text/javascript">
$(function(){
	getDataBaselist(1);
});
//分页数据
function getDataBaselist(page_index) {
	var search_text = $("#search_text").val();
	$.ajax({
		type : "post",
		url : "{:__URL('PLATFORM_MAIN/dbdatabase/databaselist')}",
		success : function(data) {
			var html = '';
			$("#total_count_num").text(data["total_count"]);
			$("#page_count_num").text(data["page_count"]);
			$("#page_count").val(data["page_count"]);
			$("#pageNumber a").remove();
			if (data.length > 0) {
				for (var i = 0; i < data.length; i++) {
					html += '<tr>';
					html += '<td><div class="cell"><label><input name="sub" type="checkbox" value="'+ data[i]["name"]+'" ></label></div></td>';
					html += '<td>'+ data[i]["name"]+'</td>';
					html += '<td class="right right-indent">'+ data[i]["rows"]+'</td>';
					html += '<td class="right right-indent">'+ data[i]["data_length_info"]+ '</td>';
					html += '<td class="center">'+ data[i]["create_time"]+ '</td>';
					html += '<td class="center">';
					//html += '<a target="_blank" href="javascript:makeTable(2, &#39;'+ data[i]["name"]+'&#39;);">优化</a>';
					html += '<a href="javascript:makeTable(1,&#39;'+ data[i]["name"]+'&#39;);">修复</a></td> ';
					html += '</tr>';
				}
			} else {
				html += '<tr align="center"><th colspan="6">暂无列表</th></tr>';
			}
			$(".table tbody").html(html);
			var totalpage = $("#page_count").val();
			if (totalpage == 1) {
				changeClass("all");
			}
/* 			var $html = pagenumShow(jumpNumber,totalpage,{$pageshow})
			$("#pageNumber").append($html); */
		}
	});
}

function batchDelete() {
	var _tables = new Array();
	$(".table tbody input[type='checkbox']:checked").each(function(i) {
			_tables[i]=$(this).val();
	});
	
	if(_tables.length ==0){
		$( "#dialog" ).dialog({
			buttons: {
				"确定,#e57373": function() {
					$(this).dialog('close');
				}
			},
			contentText:"请选择需要操作的记录",
			title:"消息提醒",
		});
		return false;
	}
	ajax_ids(_tables);
}

var flag = false;
// 备份表
function ajax_ids(_tables){
	$( "#dialog" ).dialog({
		buttons: {
			"确定": function() {
				$(this).dialog('close');
					$.ajax({
						type : "post",
						url : "{:__URL('PLATFORM_MAIN/dbdatabase/exportdatabase')}",
						data : { "tables" : _tables },
						dataType : "json",
						success : function(data) {
							if(data.status ==1 && data.message=="初始化成功"){
								backup(data.tab);
								ZENG.msgbox.show(data.message, 4, 3000);
								return;
							} else{
								ZENG.msgbox.show(data.message, 5);
							}
							
						}
					})
			},
			"取消,#e57373": function() {
				$(this).dialog('close');
			}
		},
	contentText:"是否备份？",
	});
}

//备份数据库
function backup(tab,status) {
	$.ajax({
		type : "post",
		url : "{:__URL('PLATFORM_MAIN/dbdatabase/exportdatabase')}",
		data : { "id" : tab.id, "start" : tab.start },
		dataType : "json",
		success : function(data) {
			ZENG.msgbox.show("正在备份数据库，请不要关闭窗口", 6);
			if (data.status == 1) {
				if (!$.isPlainObject(data.tab)) {
					ZENG.msgbox.show(data.message, 5);
					return;
				}else{
					ZENG.msgbox.show("正在处理"+data.tab.table+' ...', 6);
				}
				backup(data.tab, tab.id != data.tab.id);
			} else {
				if(data.status == -1){
					ZENG.msgbox.show(data.message, 5);
				}else{
					ZENG.msgbox.show("备份完成", 4, 3000);
					$("input:checkbox").prop('checked',false);
				}
			}
		}
	})
}

function makeTable(type, table){
	var _tables = new Array();
	if(table == undefined ){
		$(".table tbody input[type='checkbox']:checked").each(function(i) {
			_tables[i]=$(this).val();
		});
		if(_tables.length ==0){
			$( "#dialog" ).dialog({
				buttons: {
					"确定,#e57373": function() {
						$(this).dialog('close');
					}
				},
				contentText:"请选择需要操作的记录",
				title:"消息提醒",
			});
			return false;
		}
	}else{
		_tables[0] = table;
	}
	tableOperation(type, _tables);
}

//修复 优化
function tableOperation(type, tab){
	
	if(!flag){
		flag = true;
		if(type == 1){
			ZENG.msgbox.show("正在修复数据库，请不要关闭窗口", 6);
			$.ajax({
				type:"post",
				url:"{:__URL('PLATFORM_MAIN/dbdatabase/repair')}",
				data:{tables:tab},
				success: function (data) {
					flag = false;
					if(data.status > 0){
						ZENG.msgbox.show(data.message, 4, 3000);
					}else{
						ZENG.msgbox.show(data.message, 5);
					}
		
				}
			});
			
		}else if(type == 2){
			ZENG.msgbox.show("正在优化数据库，请不要关闭窗口", 6);
			$.ajax({
				type:"post",
				url:"{:__URL('PLATFORM_MAIN/dbdatabase/optimize')}",
				data:{tables:tab},
				success: function (data) {
					flag = false;
					if(data.status > 0){
						ZENG.msgbox.show(data.message, 4, 3000);
					}else{
						ZENG.msgbox.show(data.message, 5);
					}
				}
			});
		}
	}
}

//全选
function CheckAll(event){
	var checked = event.checked;
	$(".table tbody input[type = 'checkbox']").prop("checked",checked);
}
</script>
{/block}